package com.ozo.cloud.manage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ozo.cloud.api.manage.vo.SysRoleVO;
import com.ozo.cloud.common.base.model.BasePage;
import com.ozo.cloud.common.base.model.BaseBatchDTO;
import com.ozo.cloud.common.base.model.PageResult;
import com.ozo.cloud.common.base.model.ResultVO;
import com.ozo.cloud.common.log.annotation.OperateLog;
import com.ozo.cloud.manage.pojo.dto.ChangeStatusDTO;
import com.ozo.cloud.manage.pojo.dto.SysRolePermissionDTO;
import com.ozo.cloud.manage.pojo.dto.SysRoleQueryDTO;
import com.ozo.cloud.manage.pojo.dto.SysRoleSaveDTO;
import com.ozo.cloud.manage.service.ISysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @since 2024-07-11
 */
@Tag(name = "角色管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/role")
public class SysRoleController {

    private final ISysRoleService sysRoleService;

    @OperateLog(value = "添加角色", module = "角色")
    @SaCheckPermission("manage:role:add")
    @Operation(summary = "添加角色")
    @PostMapping("/add")
    public ResultVO<Void> add(@RequestBody @Valid SysRoleSaveDTO saveDTO) {
        sysRoleService.add(saveDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "删除角色", module = "角色")
    @SaCheckPermission("manage:role:remove")
    @Operation(summary = "删除角色")
    @DeleteMapping("/remove")
    public ResultVO<Void> remove(@RequestBody @Valid BaseBatchDTO<Long> batchDTO) {
        sysRoleService.remove(batchDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "编辑角色", module = "角色")
    @SaCheckPermission("manage:role:edit")
    @Operation(summary = "编辑角色")
    @PutMapping("/edit")
    public ResultVO<Void> edit(@RequestBody @Valid SysRoleSaveDTO saveDTO) {
        sysRoleService.edit(saveDTO);
        return ResultVO.success();
    }

    @SaCheckPermission("manage:role:page")
    @Operation(summary = "角色分页")
    @GetMapping("/page")
    public ResultVO<PageResult<SysRoleVO>> page(@Valid BasePage basePage, SysRoleQueryDTO queryDTO) {
        return ResultVO.success(sysRoleService.page(basePage, queryDTO));
    }

    @Operation(summary = "角色列表")
    @GetMapping("/list")
    public ResultVO<List<SysRoleVO>> list() {
        return ResultVO.success(sysRoleService.listAll());
    }

    @OperateLog(value = "修改状态", module = "角色")
    @SaCheckPermission("manage:role:changeStatus")
    @Operation(summary = "修改状态")
    @PutMapping("/changeStatus")
    public ResultVO<Void> changeStatus(@RequestBody @Valid ChangeStatusDTO statusDTO) {
        sysRoleService.changeStatus(statusDTO);
        return ResultVO.success();
    }

    @Operation(summary = "权限详情")
    @GetMapping("/permission/{id}")
    public ResultVO<SysRolePermissionDTO> getPermission(@PathVariable("id") Long id) {
        return ResultVO.success(sysRoleService.getPermission(id));
    }

    @OperateLog(value = "设置权限", module = "角色")
    @SaCheckPermission("manage:role:permission")
    @Operation(summary = "设置权限")
    @PostMapping("/permission")
    public ResultVO<Void> savePermission(@RequestBody @Valid SysRolePermissionDTO rolePermissionDTO) {
        sysRoleService.savePermission(rolePermissionDTO);
        return ResultVO.success();
    }

}
